module.exports = ({key = 'myFlash', defaultValue = {}} = {}) => async (ctx, next) => {
    if (!ctx.session) throw new Error('myFlash requires this koa-session middleware.');

    let data = ctx.session[key] || defaultValue;
    delete ctx.session[key];

    Object.defineProperty(ctx, key, {
        enumerable: true,
        get() {
            return data;
        },
        set(val) {
            ctx.session[key] = val;
        }
    });

    await next();

    if(302==ctx.status && ctx.session && !ctx.session[key]){
        ctx.session[key]=data;
    }
};